package com.wxy.ej.flow.modules.act.listen;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.Expression;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


/**
 * 每个人完成任务，都会执行一下。
 */
@Slf4j
@Component("clearTaskVarsListener")
@Scope("prototype")
public class ClearTaskVarsListener implements ExecutionListener {


    private Expression varPre;

    @Override
    public void notify(DelegateExecution delegateExecution) {
        log.info("---->act listener: ClearTaskVarsListener");
        System.out.println("EventName:" + delegateExecution.getEventName());

        /*// 并发网关时，变量冲突，指定下变量前缀
        String varPreStr = "";
        if (!StringUtils.isNull(varPre)){
            varPreStr = varPre.getExpressionText();
        }

        delegateExecution.removeVariable(varPreStr+"assigneeCount");
        delegateExecution.removeVariable(varPreStr+"userList");
        delegateExecution.removeVariable(varPreStr+"userNoticeList");
        delegateExecution.removeVariable(varPreStr+"companyList");
        delegateExecution.removeVariable(varPreStr+"companyIdList");
        delegateExecution.removeVariable(varPreStr+"companyNameList");

        // condition
        delegateExecution.removeVariable(varPreStr+"cmpyConditionRadio");
        delegateExecution.removeVariable(varPreStr+"cmpyConditionCount");
        delegateExecution.removeVariable(varPreStr+"cmpyConditionRate");
        delegateExecution.removeVariable(varPreStr+"userConditionRadio");
        delegateExecution.removeVariable(varPreStr+"userConditionCount");
        delegateExecution.removeVariable(varPreStr+"userConditionRate");

        // 流程执行日志
        FlowElement flowElement = delegateExecution.getCurrentFlowElement();
        // ${companyId}
        String doc = ExpressionUtils.parse(flowElement.getDocumentation(), delegateExecution);

        CtcTaskLogBizDto taskLogDto = new CtcTaskLogBizDto();
        taskLogDto.setCtcTaskId(Long.parseLong(delegateExecution.getProcessInstanceBusinessKey()));
        taskLogDto.setFlowElementId(flowElement.getId());
        taskLogDto.setFlowElementName(flowElement.getName());
        taskLogDto.setFlowElementDoc(doc);
        taskLogDto.setCreatedate(DateTool.currentDate("yyyy-MM-dd HH:mm:ss.SSS"));
        StringBuilder sb = new StringBuilder();
        sb.append("清除流程节点变量； userList、companyList、companyIdList；companyNameList");
        taskLogDto.setDescription(sb.toString());
        ctcTaskLogBizService.create(taskLogDto);
        */
    }
}
